const fs = require('fs');
const path = require('path');
const sharp = require('sharp');

const types = ['INTJ','INTP','ENTJ','ENTP','INFJ','INFP','ENFJ','ENFP','ISTJ','ISFJ','ESTJ','ESFJ','ISTP','ISFP','ESTP','ESFP'];
const outDir = path.join(__dirname, '..', 'MyPro', 'static', 'icons', 'mbti');
const size = 256;
const brand = { primary: '#6B5B95', accent: '#F7D488' };

function iconForType(t){
  switch (t) {
    // NT：理性派
    case 'INTJ':
      // 棋马（战略）
      return `
        <path d="M92 180 h72 v12 h-84 v-12 z" fill="#fff" opacity="0.9"/>
        <path d="M160 168 c-4 -24 -16 -40 -36 -48 12 -10 18 -22 18 -36 -18 6 -30 12 -44 30 -6 8 -10 18 -12 30 -10 8 -16 16 -16 24 v12 h90 z" fill="#fff"/>
        <circle cx="130" cy="96" r="6" fill="#6B5B95"/>
      `;
    case 'INTP':
      // 原子/轨道（逻辑）
      return `
        <circle cx="128" cy="128" r="12" fill="#fff"/>
        <ellipse cx="128" cy="128" rx="70" ry="32" fill="none" stroke="#fff" stroke-width="8"/>
        <ellipse cx="128" cy="128" rx="32" ry="70" fill="none" stroke="#fff" stroke-width="8" transform="rotate(30 128 128)"/>
        <ellipse cx="128" cy="128" rx="32" ry="70" fill="none" stroke="#fff" stroke-width="8" transform="rotate(-30 128 128)"/>
      `;
    case 'ENTJ':
      // 靶心+箭（指挥）
      return `
        <circle cx="108" cy="132" r="44" fill="none" stroke="#fff" stroke-width="8"/>
        <circle cx="108" cy="132" r="24" fill="none" stroke="#fff" stroke-width="8"/>
        <path d="M108 132 L196 84" stroke="#fff" stroke-width="12" stroke-linecap="round"/>
        <polygon points="196,84 176,86 188,68" fill="#fff"/>
      `;
    case 'ENTP':
      // 灯泡（辩思/创变）
      return `
        <path d="M128 84 a36 36 0 1 1 0 72 c-10 0 -14 12 -14 18 h28 c0 -6 -4 -18 -14 -18 a36 36 0 0 1 0 -72" fill="#fff" opacity="0.95"/>
        <rect x="110" y="168" width="36" height="14" rx="4" fill="#fff"/>
      `;

    // NF：理想派
    case 'INFJ':
      // 心+光点（提倡）
      return `
        <path d="M128 180 C90 150,74 132,74 110 C74 94,88 82,104 82 C116 82,124 88,128 98 C132 88,140 82,152 82 C168 82,182 94,182 110 C182 132,166 150,128 180 Z" fill="#fff"/>
        <circle cx="188" cy="76" r="6" fill="#fff"/>
      `;
    case 'INFP':
      // 心+叶（价值/真实）
      return `
        <path d="M128 178 C92 150,78 134,78 112 C78 96,90 84,106 84 C116 84,124 90,128 100 C132 90,140 84,150 84 C166 84,178 96,178 112 C178 134,164 150,128 178 Z" fill="#fff"/>
        <path d="M132 124 c18 -18 34 -18 42 -6 c-14 -2 -24 4 -36 16" fill="none" stroke="#6B5B95" stroke-width="4"/>
      `;
    case 'ENFJ':
      // 人群/连结
      return `
        <circle cx="104" cy="112" r="14" fill="#fff"/>
        <circle cx="152" cy="112" r="14" fill="#fff"/>
        <rect x="86" y="130" width="84" height="38" rx="12" fill="#fff"/>
      `;
    case 'ENFP':
      // 星闪/火花
      return `
        <path d="M128 80 l12 28 l30 4 l-24 18 l8 30 l-26 -16 l-26 16 l8 -30 l-24 -18 l30 -4 z" fill="#fff"/>
        <circle cx="178" cy="92" r="5" fill="#fff"/>
        <circle cx="82" cy="98" r="4" fill="#fff"/>
      `;

    // SJ：守护派
    case 'ISTJ':
      // 盾牌 + 勾
      return `
        <path d="M128 64 L188 86 V128 C188 164 162 186 128 204 C94 186 68 164 68 128 V86 Z" fill="none" stroke="#fff" stroke-width="8"/>
        <path d="M98 132 l18 18 l40 -40" fill="none" stroke="#fff" stroke-width="10" stroke-linecap="round"/>
      `;
    case 'ISFJ':
      // 手托心（守护）
      return `
        <path d="M80 156 h34 c10 0 28 -8 36 -12 c8 -4 22 -6 28 2 c6 8 -2 16 -14 18 l-42 8 h-42" fill="none" stroke="#fff" stroke-width="8" stroke-linecap="round"/>
        <path d="M128 126 c-10 -8 -18 -14 -18 -24 c0 -8 6 -14 14 -14 c6 0 12 4 14 10 c2 -6 8 -10 14 -10 c8 0 14 6 14 14 c0 10 -8 16 -18 24 z" fill="#fff"/>
      `;
    case 'ESTJ':
      // 清单/勾选
      return `
        <rect x="86" y="84" width="96" height="96" rx="10" fill="none" stroke="#fff" stroke-width="8"/>
        <path d="M98 112 h34" stroke="#fff" stroke-width="8"/>
        <path d="M98 138 h34" stroke="#fff" stroke-width="8"/>
        <path d="M98 164 h34" stroke="#fff" stroke-width="8"/>
        <path d="M140 112 l8 8 l20 -20" stroke="#fff" stroke-width="8" fill="none"/>
      `;
    case 'ESFJ':
      // 握手/协作
      return `
        <path d="M88 140 l22 14 c8 4 16 4 24 -2 l16 -12 l18 12" fill="none" stroke="#fff" stroke-width="8" stroke-linecap="round"/>
        <path d="M90 126 c8 -8 20 -8 30 0 l12 10" fill="none" stroke="#fff" stroke-width="8"/>
      `;

    // SP：实践派
    case 'ISTP':
      // 扳手 + 螺栓
      return `
        <path d="M94 160 l32 -32 c6 -6 16 -6 22 0 c6 6 6 16 0 22 l-32 32 l-22 -22 z" fill="#fff"/>
        <circle cx="168" cy="108" r="14" fill="none" stroke="#fff" stroke-width="8"/>
        <path d="M168 94 v8 M168 114 v8 M154 108 h8 M174 108 h8" stroke="#fff" stroke-width="6"/>
      `;
    case 'ISFP':
      // 叶片/花瓣（美感）
      return `
        <path d="M120 88 c-10 30 -10 54 10 80 c-36 -10 -54 -34 -64 -62 c20 -6 36 -12 54 -18 z" fill="#fff"/>
        <path d="M176 96 c-28 4 -50 20 -60 46 c30 0 52 -12 66 -32 c-2 -6 -4 -10 -6 -14 z" fill="#fff" opacity="0.85"/>
      `;
    case 'ESTP':
      // 闪电/旗帜（行动）
      return `
        <polygon points="116,84 156,84 136,120 166,120 110,188 126,144 98,144" fill="#fff"/>
      `;
    case 'ESFP':
      // 音符/星（表达）
      return `
        <path d="M110 92 v60 c0 10 -10 18 -22 18 s-22 -8 -22 -18 10 -18 22 -18 c6 0 12 2 16 6 v-48 l42 -10 v42 c0 10 -10 18 -22 18" fill="none" stroke="#fff" stroke-width="8"/>
        <circle cx="176" cy="98" r="6" fill="#fff"/>
      `;
    default:
      // 兜底：星
      return `<polygon points="128,70 146,110 190,116 158,146 166,190 128,168 90,190 98,146 66,116 110,110" fill="#fff" opacity="0.95"/>`;
  }
}

function svgFor(type){
  const gradId = 'g';
  const icon = iconForType(type);
  const svg = `
  <svg width="${size}" height="${size}" viewBox="0 0 ${size} ${size}" xmlns="http://www.w3.org/2000/svg">
    <defs>
      <linearGradient id="${gradId}" x1="0" y1="0" x2="1" y2="1">
        <stop offset="0%" stop-color="${brand.primary}"/>
        <stop offset="100%" stop-color="${brand.accent}" stop-opacity="0.55"/>
      </linearGradient>
    </defs>
    <rect width="100%" height="100%" rx="32" ry="32" fill="url(#${gradId})"/>
    ${icon}
    <rect x="16" y="16" width="224" height="224" rx="28" ry="28" fill="none" stroke="rgba(255,255,255,0.25)" stroke-width="4"/>
    <text x="50%" y="88%" fill="#FFFFFF" font-family="Arial, Helvetica, sans-serif" font-weight="700" font-size="36" text-anchor="middle">${type}</text>
  </svg>`;
  return Buffer.from(svg);
}

(async () => {
  if (!fs.existsSync(outDir)) fs.mkdirSync(outDir, { recursive: true });
  for (const t of types) {
    const svg = svgFor(t);
    const file = path.join(outDir, `${t.toLowerCase()}.png`);
    await sharp(svg).png({ quality: 92 }).toFile(file);
    console.log('Generated', file);
  }
})(); 